package com.lethelh.mgallery.service;

import java.util.List;

import com.lethelh.mgallery.dao.PaintingDao;
import com.lethelh.mgallery.entity.Painting;
import com.lethelh.mgallery.utils.PageModel;

public class PaintingService {
	private PaintingDao paintingDao = new PaintingDao();
	public PageModel pagination(int page,int rows,String...category) {
		if(rows == 0) {
			throw new RuntimeException("无效的rows参数");
		}
		if(category.length==0 || category[0] == null) {
			return paintingDao.pagination(page, rows);
		}else {
			return paintingDao.pagination(Integer.parseInt(category[0]), page, rows);
		}
	}
	
	public void create(Painting painting) {
		paintingDao.create(painting);
	}
	
	public Painting findById(Integer id) {
		Painting p = paintingDao.findById(id);
		if(p == null) {
			throw new RuntimeException("[id="+id+"]油画不存在");
		}
		return p;
	}
	
	public void update(Painting newPainting, int isPriviewModified) {
		Painting painting = paintingDao.findById(newPainting.getId());
		painting.setPname(newPainting.getPname());
		painting.setPrice(newPainting.getPrice());
		painting.setCategory(newPainting.getCategory());
		painting.setDescription(newPainting.getDescription());
		
		if(isPriviewModified == 1) {
			painting.setPreview(newPainting.getPreview());
		}
		
		paintingDao.update(painting);
	}
	
	public void delete(Integer id) {
		paintingDao.delete(id);
	}
	
	public static void main(String[] args) {
		PaintingService ps = new PaintingService();
		PageModel pageModel = ps.pagination(2, 6);
		List<Painting> paintList = pageModel.getPageData();
		for(Painting painting:paintList) {
			System.out.println(painting.getPname());
		}
		System.out.println(pageModel.getPageStartRow()+":"+pageModel.getPageEndRow());
	}
}
